package com.example.content.activity

import android.Manifest
import android.content.Intent
import android.graphics.Bitmap
import android.os.Environment
import android.util.Log
import com.cjt2325.cameralibrary.JCameraView
import com.cjt2325.cameralibrary.listener.JCameraListener
import com.example.content.R
import com.example.content.SELECT_TYPE
import com.example.content.VIDEO_URL
import com.example.content.databinding.ActivityCaptureVideoBinding
import com.example.content.manager.GlideEngine
import com.example.content.viewmodule.CommonViewModel
import com.google.android.material.internal.ParcelableSparseArray
import com.huantansheng.easyphotos.EasyPhotos
import com.huantansheng.easyphotos.constant.Type
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.tbruyelle.rxpermissions3.RxPermissions
import java.io.File

class CaptureVideoActivity : BaseActivity<ActivityCaptureVideoBinding, CommonViewModel>() {

    override fun initView() {
        val rxPermissions = RxPermissions(this)
        rxPermissions.request(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA
        ).subscribe()
        dataBinding.cameraView.setSaveVideoPath(
            Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM
            ).path
        )
        dataBinding.cameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER)
        dataBinding.cameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE)
        dataBinding.cameraView.setTip("长按拍摄视频")
        dataBinding.cameraView.setJCameraLisenter(object : JCameraListener {
            override fun captureSuccess(bitmap: Bitmap?) {

            }

            override fun recordSuccess(url: String?, firstFrame: Bitmap?) {
                Log.i("videoRecorder", "recordSuccess: video url: " + url)
                EasyPhotos.notifyMedia(this@CaptureVideoActivity, url)
                val intent = Intent(this@CaptureVideoActivity, UpProductActivity::class.java)
                intent.putExtra(VIDEO_URL, url)
                intent.putExtra(SELECT_TYPE, 1)
                startActivity(intent)
                finish()
            }
        })
        dataBinding.cameraView.setLeftClickListener { finish() }
        dataBinding.cameraView.setRightClickListener {
            EasyPhotos.createAlbum(this@CaptureVideoActivity, true, GlideEngine)
                .setFileProviderAuthority("com.example.pvshow.fileprovider")
                .setCount(1)
                .filter(Type.VIDEO)
                .start(REQUEST_CODE)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && data != null) {
            val images = data.getParcelableArrayListExtra<Photo>(EasyPhotos.RESULT_PHOTOS)
            val intent = Intent(this@CaptureVideoActivity, UpProductActivity::class.java)
            intent.putParcelableArrayListExtra(EasyPhotos.RESULT_PHOTOS, images)
            intent.putExtra(SELECT_TYPE, 1)
            startActivity(intent)
            finish()
        }
    }

    override fun getLayoutId() = R.layout.activity_capture_video

    companion object {
        const val REQUEST_CODE = 1001
    }
}